package com.guigu.zj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.FoodMapper;
import com.guigu.mapper.SerialcodingMapper;
import com.guigu.pojo.Food;
import com.guigu.pojo.Serialcoding;
import com.guigu.zj.pojo.PageVo;
import com.guigu.zj.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    FoodMapper foodMapper;
    @Autowired
    SerialcodingMapper slm;
    public PageVo<Food> queryAll(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getNum());
        List<Food> foods = foodMapper.queryAll(pageVo);
        PageInfo pageInfo = new PageInfo(foods);
        long total = pageInfo.getTotal();
        PageVo<Food> vo = new PageVo<Food>();
        vo.setTotal(total);
        vo.setRows(foods);
        return vo;

    }

    public int insertSelective(Food record) {
        Serialcoding serialcoding = slm.find();
        record.setBm("sp00"+(serialcoding.getFoodid()+1));
        Serialcoding s = new Serialcoding();
        s.setFoodid(serialcoding.getFoodid()+1);
        slm.update(s);
        return foodMapper.insertSelective(record);
    }

    public Food selectByPrimaryKey(Integer id) {
        return foodMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Food record) {
        return foodMapper.updateByPrimaryKeySelective(record);
    }

    public int deleteByPrimaryKey(Integer id) {
        return foodMapper.deleteByPrimaryKey(id);
    }
}
